package com.cheyunkeji.er.adapter.evaluate;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.evaluate.ArchiveSelectCarActivity;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.evaluate.CarInfoSearchedByVin;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SelectCarByVinResultAdapter extends MyBaseAdapter {


    private static final String TAG = SelectCarByVinResultAdapter.class.getSimpleName();
    private int LAST_CHECKED_POSITION = -1;
    private onItemCBoxRecheckListener listener;

    public SelectCarByVinResultAdapter(ArrayList<CarInfoSearchedByVin> dataList, Context context) {
        super(dataList, context);
        setOnItemCBoxRecheckListener((ArchiveSelectCarActivity) context);
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.er_archive_select_car_lv_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position % 2 != 0)
            convertView.setBackgroundColor(Color.parseColor("#fbfbfb"));
        else
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));

        CarInfoSearchedByVin itemBean = (CarInfoSearchedByVin) dataList.get(position);
        holder.tvBrand.setText(itemBean.getBrand());
        holder.tvSeries.setText(itemBean.getSeries());
        holder.tvModel.setText(itemBean.getSpec());
        holder.tvYearStyle.setText(itemBean.getCyear());
        holder.tvBodyStyle.setText(itemBean.getBodystyle());
        holder.tvBsx.setText(itemBean.getTrans());
        holder.tvPfbz.setText(itemBean.getEmission());
        holder.tvPl.setText(itemBean.getDisplace());
        holder.tvRllx.setText(itemBean.getFueltype());
        holder.tvQdfs.setText(itemBean.getDrivetrain());

        holder.cbAdd.setTag(position);

        holder.cbAdd.setOnCheckedChangeListener(

                new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        if (isChecked) {
                            LAST_CHECKED_POSITION = position;
                            notifyDataSetChanged();

                        } else {
                            if (position == LAST_CHECKED_POSITION) {
                                LAST_CHECKED_POSITION = -1;
                                listener.onItemChecked(LAST_CHECKED_POSITION);
                            }
                        }

                        listener.onItemChecked(LAST_CHECKED_POSITION);
                        Log.e(TAG, "onCheckedChanged:  Position : " + LAST_CHECKED_POSITION);
                    }
                });

        if (position == LAST_CHECKED_POSITION) {
            holder.cbAdd.setChecked(true);
        } else {
            holder.cbAdd.setChecked(false);
        }

        return convertView;

    }

    public void setOnItemCBoxRecheckListener(onItemCBoxRecheckListener listener) {

        this.listener = listener;
    }

    //    更新当前选中的item下标
    public void setLAST_CHECKED_POSITION(int position) {
        if (this.LAST_CHECKED_POSITION == position) {
//          反选
            LAST_CHECKED_POSITION = -1;
        } else
            this.LAST_CHECKED_POSITION = position;
    }

    public interface onItemCBoxRecheckListener {

        void onItemChecked(int position);

    }


    static class ViewHolder {
        @BindView(R.id.cb_add)
        CheckBox cbAdd;
        @BindView(R.id.tv_brand)
        TextView tvBrand;
        @BindView(R.id.tv_series)
        TextView tvSeries;
        @BindView(R.id.tv_model)
        TextView tvModel;
        @BindView(R.id.tv_year_style)
        TextView tvYearStyle;
        @BindView(R.id.tv_body_style)
        TextView tvBodyStyle;
        @BindView(R.id.tv_bsx)
        TextView tvBsx;
        @BindView(R.id.tv_pfbz)
        TextView tvPfbz;
        @BindView(R.id.tv_pl)
        TextView tvPl;
        @BindView(R.id.tv_rllx)
        TextView tvRllx;
        @BindView(R.id.tv_qdfs)
        TextView tvQdfs;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
